import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { QuestionComponent } from '../..';

import { QuestionModel } from '../../../models/question.model';


@Component({
  selector: 'qn-question-checkbox',
  templateUrl: 'question-checkbox.component.html'
})

export class QuestionCheckboxComponent extends QuestionComponent {
  @Input()
  question: QuestionModel;
  @Input()
  editable: boolean;
  @Output()
  deleteQuestionReq = new EventEmitter();

  private key: number;

  // tslint:disable-next-line:use-life-cycle-interface
  ngOnInit() {
    this.copyQuestion();
    const options = this.question.options;
    this.key = options[options.length - 1].key;
    if (!this.question.answer.seleted) {
      this.question.answer.seleted = [];
    }
  }

  setSeletdValue(checked: boolean, value: string) {
    const seleted = this.question.answer.seleted;
    const index: number = seleted.indexOf(value);
    if (checked) {
      if (index < 0) {
        seleted.push(value);
      }
    } else {
      if (index > -1) {
        seleted.splice(index, 1);
      }
    }
  }

}
